<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <link rel="stylesheet" href="/layui/css/layui.css" media="all">
</head>
<body>
<form class="layui-form" lay-filter="menu">
  <div id="id" class="layui-form-item">
    <label class="layui-form-label">id</label>
    <div class="layui-input-inline">
      <input readonly type="text" name="id" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  
  <div class="layui-form-item">
    <label class="layui-form-label">用户名</label>
    <div class="layui-input-inline">
      <input type="text" name="username" placeholder="请输入用户名" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">角色编码</label>
    <div class="layui-input-inline">
      <input type="text" name="code" placeholder="请输入编码" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">密码</label>
    <div class="layui-input-inline">
      <input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码"
             autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">姓名</label>
    <div class="layui-input-inline">
      <input type="text" name="name" placeholder="请输入姓名" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">身份证</label>
    <div class="layui-input-inline">
      <input type="text" name="identity" placeholder="请输入身份证" lay-verify="required" autocomplete="off"
             class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">手机号</label>
    <div class="layui-input-inline">
      <input type="text" name="phone" placeholder="请输入手机号" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">邮箱</label>
    <div class="layui-input-inline">
      <input type="text" name="email" placeholder="请输入邮箱" lay-verify="required" autocomplete="off" class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">微信号</label>
    <div class="layui-input-inline">
      <input type="text" name="wxAccount" placeholder="请输入微信号" lay-verify="required" autocomplete="off"
             class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">QQ号</label>
    <div class="layui-input-inline">
      <input type="text" name="qqAccount" placeholder="QQ号" lay-verify="required" autocomplete="off"
             class="layui-input">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">性别</label>
    <div class="layui-input-inline">
      <input id="man" type="radio" name="sex" value="男" title="男" checked>
      <input id="woman" type="radio" name="sex" value="女" title="女">
    </div>
  </div>
  
  <div class="layui-form-item">
    <label class="layui-form-label">是否启用</label>
    <div class="layui-input-inline">
      <input type="checkbox" name="state" lay-skin="switch" lay-text="ON|OFF" checked>
    </div>
  </div>
  
  <div class="layui-form-item layui-form-text">
    <label class="layui-form-label">备注</label>
    <div class="layui-input-block">
      <textarea name="remark" placeholder="请输入" class="layui-textarea"></textarea>
    </div>
  </div>
  
  <div class="layui-form-item">
    <div class="layui-input-block">
      <button id="insert" class="layui-btn" lay-submit lay-filter="insert">立即提交</button>
      <button id="update" class="layui-btn" lay-submit lay-filter="update">立即提交</button>
      <button id="reset" type="reset" class="layui-btn layui-btn-primary">重置</button>
    </div>
  </div>
</form>
<script src="/layui/layui.js" charset="utf-8"></script>
<script src="/jquery/jquery-3.5.1.min.js"></script>
<script>
  // 先初始化
  layui.use(['form', 'layer'], function () {
    const form = layui.form;
    const layer = layui.layer;
    // 新增
    form.on('submit(insert)', function (data) {
      const params = JSON.stringify(data.field);
      $.ajax({
        url: "/api/customer",
        method: 'post',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 200) {
            layer.msg('添加成功')
            // 1秒后刷新
            setTimeout(function () {
              window.parent.location.reload();
            }, 1500);
          } else if (data.code === 100) {
            layer.msg(data.message);
          }
          ;
        }
      });
      return false;
    });
    // 修改
    form.on('submit(update)', function (data) {
      const params = JSON.stringify(data.field);
      const id = data.field.id;
      $.ajax({
        url: "/api/customer/" + id,
        method: 'put',
        contentType: 'application/json',
        data: params,
        dataType: 'json',
        success: function (data) {
          if (data.code === 200) {
            layer.msg('修改成功')
            setTimeout(function () {
              window.parent.location.reload();
            }, 1500);
          } else if (data.code === 100) {
            layer.msg(data.message);
          }
        }
      });
      return false;
    })
  });
  
  // 最后赋值
  function formData(data) {
    layui.use(['form', 'layer'], function () {
      const form = layui.form;
      form.val("menu", JSON.parse(JSON.stringify(data)));
    });
  }
</script>
</body>
</html>